package com.xbn.xbnmall.product.service.impl;

import com.xbn.xbnmall.product.entity.AttrGroupEntity;
import com.xbn.xbnmall.product.entity.SkuImagesEntity;
import com.xbn.xbnmall.product.entity.SpuInfoDescEntity;
import com.xbn.xbnmall.product.service.*;
import com.xbn.xbnmall.product.vo.SkuItemSaleAttrVo;
import com.xbn.xbnmall.product.vo.SkuItemVo;
import com.xbn.xbnmall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.product.dao.SkuInfoDao;
import com.xbn.xbnmall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key))
        {
            wrapper.and(wrapper1->{
                wrapper1.eq("sku_id",key).or().like("sku_name",key).or().
                        like("sku_desc",key);
            });
        }
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId))
        {
            wrapper.eq("brand_id",brandId);
        }

        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&Integer.valueOf(catelogId)!=0)
        {
            wrapper.eq("catalog_id",catelogId);
        }

        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min))
        {
            try {
                BigDecimal bigDecimal=new BigDecimal(min);
                //售价的最低值要比0大才生效
                if(bigDecimal.compareTo(new BigDecimal("0"))==1)
                {
                    wrapper.ge("price",min);
                };
            }catch (Exception e)
            {

            }

        }
        String max = (String) params.get("max");
        if(!StringUtils.isEmpty(max)&&Integer.valueOf(max)!=0)
        {
            try {
                BigDecimal bigDecimal=new BigDecimal(max);
                //售价的最低值要比0大才生效
                if(bigDecimal.compareTo(new BigDecimal("0"))==1)
                {
                    wrapper.le("price",max);
                };
            }catch (Exception e)
            {

            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> list = this.list(
                new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId)
        );
        return list;
    }

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo=new SkuItemVo();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //查询sku的基本信息，pms_sku_info
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, threadPoolExecutor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((result) -> {
            //获取sku的销售组合属性
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(result.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, threadPoolExecutor);

        CompletableFuture<Void> descuFture = infoFuture.thenAcceptAsync((result) -> {
            //spu的介绍查询
            Long spuId = result.getSpuId();
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(spuId);
            skuItemVo.setDesc(spuInfoDesc);
        }, threadPoolExecutor);

        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((result) -> {
            //获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(result.getSpuId()
                    , result.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, threadPoolExecutor);


        //获取sku的图片信息，pms_sku_images
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImages = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(skuImages);
        }, threadPoolExecutor);
        //等待所有任务全部都运行完了再去返回结果
        try {
            CompletableFuture.allOf(saleAttrFuture,descuFture,
                    baseAttrFuture,imageFuture).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return skuItemVo;
    }
}